Central Article

Creating Your First Macromedia Central Application


Table of Contents

  1. Introduction
  2. Structure and Terms Before Getting Started
  3. Creating a Simple Central Application
  4. Updating Your Central Application
  5. Creating an Agent
  6. Turning a Simple Application into Something Useful

Creating an Agent

Your Central product can include several applications. (You might call the product a "suite" in that case.) For each application you can have multiple pods; in fact, after users install the application you can add the pods dynamically—without their intervention. Besides pods, your application can include up to one agent.

An agent is a SWF that's never seen. However, you can set your agent to start every time that Central does. This means code continues executing even when your application is closed and Central is minimized. Agents are useful for performing background tasks such as checking current weather conditions. When the temperature drops, for example, you could inform the user through a notice that behaves almost like an instant message. Not only are agents good for behind-the-scenes work but they're the logical place to store code that's common to your application and pods. This way it's available anytime the application needs it.

Sometimes commands are only available to pods, such as loadApplication(). Other times only applications can trigger them, such as requestPayment(). Generally, the agent—and its "shell" the Agent Manager—is the common denominator: It runs constantly and most commands are available there.

The ways that your application, pods, and agent talk to Central is through their respective managing objects: Shell, Console, and Agent Manager. You can see in Table 1 that most of the methods are available for all three objects. That is, you can put code where you want. However, some methods don't apply to certain situations. For example, a pod doesn't need to use isConsoleOpen() to find out whether the Console is open because the Console must be open in the first place for the pod to find out. There are other logical restrictions where one object doesn't support a particular method, but methods are available where you need them. For the most part it's up to you whether you trigger a method from your application, agent, or pod(s).

c
Table 1. Methods Supported by Agent Manager, Shell, and Pod
Methods Agent Manager Shell Pod
Notices      
addNotice Yes Yes Yes
getNotices Yes Yes Yes
removeNotice Yes Yes Yes
Local Internet Cache      
addToLocalInternetCache Yes Yes Yes
inLocalInternetCache Yes Yes Yes
removeFromInternetCache Yes Yes Yes
Information      
isConsoleOpen Yes Yes No
isConnected Yes Yes Yes
getPreferences Yes Yes Yes
getHeight No No Yes
getLastTabIndex No No Yes
Pod Management      
getPods Yes Yes Yes
addPod Yes Yes Yes
viewPod Yes Yes Yes
removePod Yes Yes Yes
getViewedPods Yes Yes Yes
Application Management      
getViewedApplications Yes Yes Yes
loadApplication No No Yes
Agent Management      
startAgent No Yes Yes
stopAgent Yes Yes Yes
getAgent No Yes Yes
Miscellaneous      
editLocationDialog No Yes Yes
newLocationDialog No Yes Yes
requestSizeChange No Yes No
validateActivationKey No Yes No
requestPayment No Yes No
getBounds No Yes No
getSelectedItem No Yes No
setProgress No Yes No
setStatus No Yes No
isPurchased No Yes No

While it's impossible to put all your code in the agent, you should definitely try to keep most common elements here. Ultimately, you need to send messages among your agent, pod, and application. The channel over which the pieces communicate is the Local Connection object. You can use the existing version in Macromedia Flash, but Macromedia Central has a much more eloquent version called LCService. This class not only simplifies managing multiple connections (say, many pods and application instances) but, because it's synchronous, you can set up methods that return values. I use it next to let your application and pods trigger code in the agent, as well as let the agent trigger code in the application and pods.

Note: Just in case you're thinking of using local shared objects to make the different pieces of your product communicate with each other, don't. You'll run into problems if two files attempt to read and write at the same time. Besides, the LCService class makes this task very easy. For more information about LCService, read Ethan Malasky's article, "Understanding and Using the LCService Library."

In the next exercise I show you that adding an agent is nearly the same as making the application or pod. The key is to select the appropriate tasks for your agent to perform. Generally speaking, this includes tasks you want to execute independently of whether the user is actively running your application or pod. In your agent, for example, you could use setInterval() to trigger a function that accesses a web service every 10 minutes and finds out the current temperature in the user's city. Of course, the user would need to use either a pod or application as an interface to set their city. Similarly, if the agent reports the results it gathers it needs to send that information to a pod, application, or—perhaps—a notice. Agents don't start and stop as your application is opened and closed. Therefore, carefully deciding what tasks your agent executes (anytime Central is open) is both an opportunity and a responsibility.

The basic idea behind LCService is that you want "clients" (the pods and application) to access methods in the "server" (the agent). Conversely, the agent should have access to methods in the pods and application. Because it's all handled synchronously, the pods, application, and agent can trigger—and get values returned—from any method in your product. You just need to specify which methods to expose to the LCService. The following steps show you how to define the methods in a file called interface.as:

  1. Create a new ActionScript file in Flash MX Professional 2004 or in a text editor. Save the file as interface.as. Insert the following code:

    myInterface=new Object();
    myInterface.name="myInterfaceName";
    myInterface.interfaces=new Object();
    myInterface.interfaces.Client=["tellEveryone"];
    myInterface.interfaces.Server=["agentMethod", "forwardMethod"];
    

    Here's the simple way to read this code: We're planning to have one method in the application and pods (tellEveryone) and two methods in the agent (agentMethod and forwardMethod). The LCService class uses a client/server metaphor where the agent is the server and the pods and application are the client. Each file in your product needs the same interface definition—which will be easy to include now that you have a separate file (interface.as).

  2. Create a new file, as you did in the earlier exercises. This time don't bother with anything on the Stage because the agent is never visible. Just set the publish settings to Flash Player 6 and uncheck the HTML format.

    Also, put this code in the first frame:

    #include "interface.as"
    #include "agent.as"

  3. Save this file as agent.fla.
  4. Create a new ActionScript file and save it as agent.as, adjacent to agent.fla.
  5. Type the following code:

    1  owner=this;
    2  agentMethod=function(param){
    3    return "I heard "+param;
    4  }
    5  forwardMethod=function(param){
    6    myService.tellEveryone(param);
    7  }
    8  onActivate = function(agentManager, agentID, data) {
    9    gShell=agentManager;
    10   myService=Central.LCService.createServer(myInterface, owner, true);
    11}
    12 Central.initAgent (this, this);
    

    The onActivate() and initAgent() methods should look familiar. The main thing in onActivate() is creating a server instance of the LCService class. The myService variable is the way the agent triggers code in the pod or application (as on line 6). The createServer() method specifies the myInterface variable (defined in the interface.as file) and passes a reference to the place where server methods will be found (this timeline) and opt for the synchronous behavior (in the true parameter).

    Also, recall from interface.as that the two methods (agentMethod and forwardMethod) are exposed to the clients—that is, the pod and application. Just as how line 6 accesses the tellEveryone() method (to be defined in the application and pod) using a reference to the LCService, the pod and application similarly access agentMethod() and forwardMethod().

  6. Save agent.as, return to agent.fla, and publish agent.swf (press Shift-F12).
  7. Open product.xml in order to add the pod and increment the version number. Specifically, change this line:

    version="1.1"

    so it reads:

    version="1.2"

    Also change this line:

    <!--<agent name="Sample Agent" src="agent.swf" started="true"/>-->

    so it reads:

    <agent name="Sample Agent" src="agent.swf" started="true"/>

  8. Save product.xml.

Notice that the started attribute means that this agent launches as soon as Central does. (Otherwise you have to start it with the startAgent() command.)

Before you actually update the application, first update the pod and application files so that they use the agent:

  1. Open pod.as and replace all the code with the following:

    owner=this;
    tellEveryone=function(param){
      ta.text+=param+newline;
    }
    onActivate = function(console, podID, viewerID, position, baseTabIndex, data) {
      gShell=console; 
      var uniqueID = podID+"_"+viewerID;
    
      myService=Central.LCService.createClient(myInterface, uniqueID, owner, true);
    
    }
    Central.initPod(this, this);
    

    In this case we're creating a client version of the LCService class, saved in the myService variable. Like the createServer() method, createClient() needs the variable containing the interface (myInterface—which gets defined in interface.as) and object where the methods will reside (owner, that is this timeline) and whether you want it to behave synchronously (true). The second parameter sent (uniqueID), however, is a unique string to prevent clients from conflicting. It’s not needed with createServer() because there can be only one "server."

    The tellEveryone() function is simply the pod's implementation of this method. That is, the application has the same method but it executes different code. Finally, although the application triggers the agent's forwardMethod(), coming up in Step 4 below, nowhere does the pod itself access either of the two agent's methods.

  2. Save pod.as and open pod.fla. Edit the script to read as follows:

    #include "interface.as"
    #include "pod.as"

  3. Export it to create pod.swf.
  4. Open app.as and change the code to read as follows:

    owner=this;
    tellEveryone=function(param){
      ta.text+=param+newline;
      gShell.setStatus(param);
    }
    echo_btn.onPress=function(){
      ta.text+=myService.agentMethod(input_txt.text);
    }
    send_btn.onPress=function(){
      myService.forwardMethod(input_txt.text);
    }
    onActivate = function(shell, appID, shellID, baseTabIndex, data) {
      gShell=shell; 
      var uniqueID = appID+"_"+shellID;
    
      myService=Central.LCService.createClient(myInterface, uniqueID, owner, true);
    
    }
    Central.initApplication(this, this);

    This is just a bit more than the pod's code. Notice that the implementation of tellEveryone is slightly different than the pod's version. (Here you change the text in the shell's status bar.) There are also two button instances in the movie (echo_btn and send_btn) plus a text field instance (input_txt). The echo button just proves that you can call a method in the agent and get a return value. The send button is a bit simpler: It just passes data to the agent, although the agent turns around and sends that data back through the tellEveryone method. (In the last part of this tutorial you modify the agent's forwardMethod so that it looks up the current temperature for a ZIP code that you send it using this button.)

  5. Return to app.fla and modify the code to read as follows:

    #include "interface.as"
    #include "app.as"

  6. Before you publish, place two buttons on the Stage (instance names echo_btn and send_btn) and one input text field (named input_txt). Then publish it to create app.swf.
  7. Test it out! Launch the application, check for updates, and install the new version. Be sure the pod is open and try typing into the input text field and clicking either button. The send_btn passes the string to the agent, which then sends it back to both the pod and application (with the application also changing the status text area). The echo_btn simply gets a value back from the agent and places it into the TextArea component.

    Note: If you have problems with this portion, the completed files are in the folder "part3_finished--Creating an Agent.".